package GUI.gameWinOld;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.LinkedList;

import game.GameBoard;
import game.Player;

import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

import GUI.gameWin.CardHorizontalLayout;
import GUI.gameWin.CardVerticalLayout;

import carddeck.Card;

@SuppressWarnings("serial")
public class CardPanel extends JPanel implements CardPanelInterface {
	private Player playerOwner;
	private GameBoard gameHandler;

	private boolean horizontal;

	public CardPanel(GameBoard game, Player player, boolean bHorizontal) {
		super();
		gameHandler=game;
		playerOwner = player;
		setBackground(new Color(0, 77, 0));
		//setLayout(null);
		setBorder(new TitledBorder(playerOwner.getName()));
		((TitledBorder) getBorder()).setTitleColor(Color.WHITE);
		horizontal=bHorizontal;
		if(horizontal) {
			setLayout(new CardHorizontalLayout(15));
			setSize(new Dimension(540,100));
			setMinimumSize(new Dimension(540,100));
		}
		else {
			setLayout(new CardVerticalLayout(15));
			setSize(new Dimension(100, 540));
			setMinimumSize(new Dimension(100, 540));
		}
		setVisible(true);
		setOpaque(true);
	}

	public CardPanel() {
		super();
	}
	
	@Override
	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		super.paint(g);

	}

	@Override
	public void refresh() {
		removeAll();
		LinkedList<Card> playerCards = playerOwner.getCards();
		for (int i = 0; i<playerCards.size()-1;i++) {
			Card theCard = playerCards.get(i);
			CardButton theButton = new CardButton(playerOwner, gameHandler, theCard, false, horizontal);
			add(theButton);
		}
		if(!playerCards.isEmpty()) {
			Card theCard = playerCards.getLast();
			CardButton theButton = new CardButton(playerOwner, gameHandler, theCard, true, horizontal);
			add(theButton);
		}
		repaint();
		revalidate();
	}
	
	public void select() {
		if(playerOwner.getWaitTime()>0)
			return;
		setBackground(new Color(0, 145, 0));
		repaint();
	}
	
	public void deselect() {
		if(playerOwner.getWaitTime()>0)
			return;
		setBackground(new Color(0, 77, 0));
		repaint();
	}
	public void setWaitTime(int waitTime) {
		if(waitTime>0) {
			setBackground(new Color(0, 0, 0));
			((TitledBorder) getBorder()).setTitle(playerOwner.getName()+" (czeka "+waitTime+")");
		}
		else if(waitTime<=0)
		{
			setBackground(new Color(0, 77, 0));
			((TitledBorder) getBorder()).setTitle(playerOwner.getName());
		}
		repaint();
	}
}
